'use strict';

const Controller = require('egg').Controller;

class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = 'hi, egg';
  }

  // 获取身份列表
  async getIdentityList() {
    const { ctx, app } = this;

    const data = await app.mysql.select('identitylist')

    ctx.body = {
      code: 1,
      data
    }
  }

  // 添加身份
  async addIdentityList() {
    const { ctx, app } = this;

    const { username } = ctx.request.body;

    await app.mysql.insert('identitylist', {
      name: username,
      auth: '100,101'
    })

    ctx.body = {
      code: 1,
      msg: '添加成功'
    }
  }

  // 添加用户
  async addUserList() {
    const { ctx, app } = this;
    const { Identity, password, username, name } = ctx.request.body;

    // 通过 Identity 获取身份权限
    let IdentityInfo = await app.mysql.get('identitylist', { id: Identity });

    // 获取权限
    const auth = IdentityInfo.auth

    // 将获取到的权限 以及 用户名 账户名 密码保存到数据库钟
    await app.mysql.insert('userlist', {
      password,
      name,
      username,
      auth,
      Identity
    })

    ctx.body = {
      code: 0,
      msg:'添加成功'
    }
  }

  // 更改身份视图权限
  async editIdentityAuth() {
    const {app, ctx} = this;

    const { username, viewAuth } = ctx.request.body;

    // 通过  username 更新数据库中的身份权限
    await app.mysql.update('identitylist', {id: username,auth: viewAuth.join(',')});

    // 获取用户列表中 依赖 该该身份的所有信息
    
    // 先获取用户列表中的数据
    const userList = await app.mysql.select('userlist');

    userList.forEach(async (item) => {
      if(item.identity === username) {
        // 触发sql 更改权限
        await app.mysql.update('userlist', {id: item.id, auth: viewAuth.join(",")})
      }
    })


    ctx.body = {
      code: 1,
      msg: '更改成功'
    }
  }

  // 获取考试类型
  async getExamType() {
    const { app, ctx } = this;

    const data = await app.mysql.select('examtype')

    ctx.body = {
      code: 1,
      data
    }
  } 

    // 获取课程类型
    async getCalssType() {
      const { app, ctx } = this;
  
      const data = await app.mysql.select('classtype')
  
      ctx.body = {
        code: 1,
        data
      }
    } 

    // 添加试题
    async addExamList() {
      const { app, ctx } = this;

      await app.mysql.insert('examlist', ctx.request.body);

      ctx.body = {
        code: 1,
        msg: '添加成功'
      }
    }

    // 考试列表
    async getExamList() {
      const {app, ctx}  =this;

      // 获取前端传来的参数
      const {classType, examType } = ctx.request.body;
      
      let data = []

      if(classType && examType) {
        data = await app.mysql.query(`select * from examlist where classType="${classType}" and examType="${examType}"`)
      } else if(classType) {
        data = await app.mysql.query(`select * from examlist where classType="${classType}"`)
      } else if (examType) {
        data = await app.mysql.query(`select * from examlist where examType="${examType}"`)
      } else {
          data = await app.mysql.select('examlist')
      }

      ctx.body = {
        code: 1,
        data
      }
    }
}

module.exports = HomeController;
